package com.ssm.modules.controller;

import com.ssm.common.bean.VResponse;
import com.ssm.modules.entity.Grade;
import com.ssm.modules.entity.Role;
import com.ssm.modules.entity.User;
import com.ssm.modules.service.IGradeService;
import com.ssm.modules.service.IRoleService;
import com.ssm.modules.service.IUserService;
import com.ssm.modules.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IGradeService gradeService;



    /**
     *  查询所有用户
     * @param page
     * @param pageSize
     * @param map
     * @return
     */
    @RequestMapping(value = "/findAllUser", method = RequestMethod.GET)
    public String findAllUser(@RequestParam(value = "page" ,defaultValue = "-1")int page,
                              @RequestParam(value = "pageSize" ,defaultValue = "15")int pageSize,
                              Map<String, Object> map) {
        //  查询用户
        List<User> allUser = userService.findAllUser(page, pageSize);
        //  获取总数
        int amount = userService.findAllUserCount();
        List<Grade> grades = gradeService.selectAll();
        List<Role> allRole = roleService.findAllRole();
        map.put("grades", grades);
        map.put("allRole", allRole);

        map.put("page", page);
        map.put("allUser", allUser);
        map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
        return "/admin/userList";
    }

    /**
     *  跳转到添加用户页面
     * @param map
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public String addUser(Map<String, Object> map) {
        try {
            List<Role> allRole = roleService.findAllRole();
            List<Grade> allGrade = gradeService.selectAll();
            map.put("allRole", allRole);
            map.put("allGrade", allGrade);
            return "addUser";
        }catch (Exception e) {
            return "error/404";
        }
    }

    /**
     *  添加用户
     * @param user
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public VResponse<Object> addUser(@RequestBody User user,
                                     Map<String, Object> map) {
        try {
            String userName = user.getUserName();
            String plainPassword = user.getPassword();
            String email = user.getEmail();
            String name = user.getName();
            Role role = user.getRole();
            if(userName == null || plainPassword == null || email == null || name == null || role.getId() == 0) {
                return VResponse.error(0, "信息不全");
            }
            User hasUser = userService.findByUserName(userName);
            if(hasUser != null) {
                return VResponse.error(0, "该用户已经存在");
            }
            //  对密码加密
            String password = SecurityUtils.entryptPassword(plainPassword);
            //  把密码更换成加密的密码
            user.setPassword(password);
            userService.addUser(user);
            roleService.userAddRole(user.getId(), role.getId());

            return VResponse.success("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            return VResponse.error(0, "注册失败");
        }
    }
    @ResponseBody
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public VResponse<Object> updateUser(@RequestBody User user){
        try {
            this.userService.updateUser(user);
            //  todo 更新角色
            if(user.getRole().getId() != 0) {
                userService.addRole(user.getId(), user.getRole().getId());
            }
            //  todo 更新班级
            if(user.getGrade().getId() != 0) {
                userService.addGrade(user.getId(), user.getGrade().getId());
            }
            if(user.getRole().getId() != 3) {
                userService.deleteGrade(user.getId());
            }
            return VResponse.success("修改成功");
        }catch (Exception e){
            return VResponse.error(0, "修改失败");
        }

    }
    @ResponseBody
    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    public VResponse<Object> deleteUser(@RequestBody User user){
        this.userService.deleteUser(user.getId());
        return VResponse.success("删除成功");
    }
}

